<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Cate as CateModel;
use app\admin\model\Article as ArticleModel;
class Cate extends Base
{
    //前置操作，所有方法执行前都要匹配该项下的规则
    protected $beforeActionList = [
        // 'first'，//逗号之后没有东西，执行一切方法之前一定先执行first方法
        // 'second' =>  ['except'=>'hello'],//除了执行hello方法外，执行其他方法都会先执行second方法
        'delsoncate'  =>  ['only'=>'del'],//只有执行del方法时才执行delsoncate方法
    ];

    public function lst()
    {
        $cate = new CateModel();
        if(request()->isPost()){
            $sorts = input('post.');
            foreach ($sorts as $k => $v) {
                $cate->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('lst'));
            return;
        }
        $cates = $cate->catetree();
        $this->assign('cates',$cates);
        return view();
    }

    public function add()
    {
        $cate = new CateModel();
        if(request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Cate');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $add = $cate->save($data);
            if($add){
                $this->success('添加栏目成功！',url('lst'));
            }else{
                $this->error('添加栏目失败！');
            }
        }
        $cates = $cate->catetree();
        $this->assign('cates',$cates);
        return view();
    }

    public function edit(){
        $cate = new CateModel();
        if(request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Cate');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $save = $cate->save($data,['id'=>$data['id']]);
            if($save !== false){
                $this->success('修改栏目成功！',url('lst'));
            }else{
                $this->error('修改栏目失败！');
            }
            return;
        }
        $cates = $cate->find(input('id'));
        $cateres = $cate->catetree();
        $this->assign(array(
            'cateres'=>$cateres,
            'cates'=>$cates,
        ));
        return view();
    }

    public function del(){
        $id = input('id');
        if(db('Cate')->delete($id)){
            $this->success("删除栏目成功！", 'lst');
        }else{
            $this->error('删除栏目失败');
        }
    }

    //无限极删除
    //删除一级时，子级要全部删掉
    public function delsoncate(){
        $cateid = input('id'); //要删除的当前栏目的id
        $cate = new CateModel();
        $sonids = $cate->getchildrenid($cateid);
        //当前栏目下的所有子栏目
        $allcateid = $sonids;
        //当前项目自己的id
        $allcateid[] = $cateid;
        foreach ($allcateid as $k=>$v) {
            $article = new ArticleModel;
            $article->where(array('cateid'=>$v))->delete();
        }
        //有子栏目才能进行删除
        if($sonids){
            db('cate')->delete($sonids);
        }
    }
}
